package com.guojian.sso.filter.chain;

import java.io.IOException;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * @author GuoJian 过滤器的链条，每次调用doFilter都会寻找下一个filter来进行处理
 *
 */
public class SsoFilterChain implements FilterChain {

	private List<Filter> filterList;

	private int index = 0;

	public SsoFilterChain(List<Filter> filterList) {
		super();
		this.filterList = filterList;
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {

		if (getFilterList() == null || this.index > getFilterList().size() - 1) {
			return;
		}
		
		try {
			filterList.get(this.index++).doFilter(request, response, this);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public List<Filter> getFilterList() {
		return filterList;
	}

	public void setFilterList(List<Filter> filterList) {
		this.filterList = filterList;
	}

}
